import { ConfigEnv, defineConfig, loadEnv } from 'vite'
import { setupVitePlugins } from './build'
import pkg from './package.json'
import { resolve } from 'path'
import moment from 'moment'

const { dependencies, devDependencies, name, version } = pkg
const __APP_INFO__ = {
  pkg: { dependencies, devDependencies, name, version },
  lastBuildTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
}

export default defineConfig((configEnv: ConfigEnv): any => {
  const viteEnv = loadEnv(configEnv.mode, process.cwd())
  console.log(viteEnv)
  return {
    base: viteEnv.VITE_BASE_URL,
    resolve: {
      alias: {
        // 别名
        '@': resolve(__dirname, './src')
      }
    },
    server: {
      // 本地服务
      host: '0.0.0.0',
      port: 8000,
      open: true,
      https: false,
      // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
      proxy: {}
    },
    plugins: setupVitePlugins(viteEnv),
    build: {
      // 打包构建相关
      sourcemap: true,
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
        }
      }
    },
    define: { __APP_INFO__: JSON.stringify(__APP_INFO__) }
  }
})
